package com.cn.bulu.TEST;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.math.RoundingMode;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.text.DecimalFormat;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.junit.Test;

import com.cn.bulu.constant.PayMethod;
import com.cn.bulu.constant.TypeOfIndent;
import com.cn.bulu.utils.DateUtils;
import com.cn.bulu.utils.LuKeOrderNoUtil;
import com.cn.bulu.utils.ClothCurtain.Arith;

public class GetOrderNo {

	DBHelp db = new DBHelp();
	private static Random random = new Random();
	private static ExecutorService executorService = new ThreadPoolExecutor(40, 50, 0L, TimeUnit.MILLISECONDS,
			new LinkedBlockingQueue<Runnable>(100000), new ThreadPoolExecutor.CallerRunsPolicy());

	public static void main(String[] args) {
		/*DecimalFormat formater = new DecimalFormat("#0.#");
		formater.setRoundingMode(RoundingMode.FLOOR);
		System.err.println(Arith.mul(2340, Arith.sub(1, 0.006)));
		System.err.println(formater.format(Arith.mul(2340, Arith.sub(1, 0.006))));*/
		
		System.err.println();
		
	}

	@Test
	public void test1() {
		String orderID = "W" + DateUtils.getDate("yyyyMMddHHmmss") + "01"
				+ (System.currentTimeMillis() + "").substring(1) + (System.nanoTime() + "").substring(7, 10);
		System.out.println(orderID);
		System.out.println(orderID.length());
	}

	@Test
	public void test2() throws Exception {
		String fileName = "E://outputError.txt";
		File file = new File(fileName);

		if (!file.exists()) {
			file.createNewFile();
		}

		FileOutputStream fos = new FileOutputStream(fileName);
		PrintStream ps = new PrintStream(fos);
		System.setErr(ps);
		final String sql = "insert into test_order(orderID) values(?)";
		long start = System.currentTimeMillis();
		for (int i = 0; i <= 10000000; i++) {
			final String insertI = String.valueOf(i);
			executorService.submit(new Runnable() {
				@Override
				public void run() {
					Connection con = null;
					PreparedStatement pstm = null;
					try {

						String orderID = "W" + DateUtils.getDate("yyMMddHH") + getRandomNumber(100, 999) + "01"
								+ (System.currentTimeMillis() + "").substring(1) + getRandomNumber(100, 999);
						System.out.println("插入订单号：" + orderID + "  尾数:" + orderID.length());
						con = db.getCon();
						pstm = con.prepareStatement(sql);
						pstm.setString(1, orderID);
						pstm.execute();
						db.closeAll(con, pstm, null);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			});
		}
		executorService.shutdown(); // 该方法在加入线程队列的线程执行完之前不会执行。
		while (true) {
			if (executorService.isTerminated()) {
				System.out.println("所有的子线程都结束了！");
				long end = System.currentTimeMillis();
				System.out.println("执行时长： " + (end - start));
				break;
			}
		}
	}

	public static int getRandomNumber(int min, int max) {
		int s = random.nextInt(max) % (max - min + 1) + min;
		return s;
	}
}
